package com.shujia.base;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class HiveJDBC {
    public static void main(String[] args) throws Exception {
        //注册驱动
        Class.forName("org.apache.hive.jdbc.HiveDriver");


        //创建数据连接对象
        Connection conn = DriverManager.getConnection("jdbc:hive2://192.168.233.101:10000");

//        System.out.println("成功与hiveserver2服务连接：" + conn);

        //获取hive操作对象
        Statement state = conn.createStatement();

        //执行sql语句
        ResultSet resultSet = state.executeQuery("select * from bigdata32.students limit 10");

        //解析查询结果
        while (resultSet.next()) {
            String id = resultSet.getString(1);
            String name = resultSet.getString(2);
            String age = resultSet.getString(3);
            String gender = resultSet.getString(4);
            String clazz = resultSet.getString(5);
            System.out.println(id+","+name+","+age+","+gender+","+clazz);
        }

        //关闭资源
        state.close();
        conn.close();
    }
}
